<?php

namespace App\Http\Controllers\Admin\newWork;

use App\Http\Config\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CatController extends CommonController
{
    private $catTableName = "new_work_cat";
    private $typeTableName = "new_work_type";
    private $catField = [
        "name" => "require", "remark" => "require", "icon" => "require", "picture" => "require",
        "intro" => "require", "form_id" => "require", "sort" => "0", "status" => 1
    ]; // 填入数据库需要存储的表字段;
    private $catFieldMsg = [
        "name" => "分类名", "remark" => "备注说明", "icon" => "图标", "picture" => "配图",
        "intro" => "内容介绍", "form_id" => "所属表单ID"
    ];
    private $data = [];
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    // 服务分类列表
	public function getPageList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $pageNo = $request->post("pageNo", 1);
        $pageSize = $request->post("pageSize", 10);
        $res = DB::table($this->catTableName. " as c")->select("c.*", "sf.name as formName")
            ->leftJoin('setting_form as sf','c.form_id','=','sf.id')
            ->where("c.mall_id", $request->mallData['id']);
        $data = array(
            'total' => $res->count(),
            'rows' => $res->skip(($pageNo-1) * $pageSize)->take($pageSize)->orderBy("c.sort", "desc")->get()
        );
        $data['rows'] = $this->transferFields(json_decode(json_encode($data['rows']),true));
        foreach ($data["rows"] as &$item){
            $item["icon"] = $this->processingPictures($item["icon"]);
        }
        $this->returnData['data'] = $data;
        return $this->return_result($this->returnData);
    }

    public function getAll(){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $data = DB::table($this->catTableName)->where("status", 1)->where("mall_id", request()->mallData['id'])->orderBy("sort", "desc")->select("id", "name")->get();
        $data = json_decode(json_encode($data),true);
        $this->returnData['data'] = $data;
        return $this->return_result($this->returnData);
    }

    // 创建服务分类
    public function create(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $params = $request->post();
        foreach ($this->catField as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !isset($params[$ks])){
                return $this->return_result(ErrorCode::$admin_enum["customized"], "{$this->catFieldMsg[$k]}不能为空");
            }
            $this->data[$k] = isset($params[$ks]) ? trim($params[$ks]) : $v;
        }
        $this->data['mall_id'] = $request->mallData['id'];
        $this->data["create_time"] = date("Y-m-d H:i:s");
        $res = DB::table($this->catTableName)->insert($this->data);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '添加失败';
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = '添加成功';
        return $this->return_result($this->returnData);
    }

    // 服务分类详情
    public function getInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        if(empty($request->id)){
            return $this->return_result(ErrorCode::$admin_enum["customized"], "ID不能为空");
        }
        $res = DB::table($this->catTableName)->where("id", $request->id)->first();
        $res = json_decode(json_encode($res),true);
        $res["icon"] = $this->processingPictures($res["icon"]);
        $res["picture"] = $this->processingPictures($res["picture"]);
        $this->returnData['data'] = $this->transferFields($res);
        return $this->return_result($this->returnData);
    }

    public function update(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $params = $request->post();
        if(empty($params["id"])){
            return $this->return_result(ErrorCode::$admin_enum["customized"], "ID不能为空");
        }
        foreach ($this->catField as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !isset($params[$ks])){
                return $this->return_result(ErrorCode::$admin_enum["customized"], "{$this->catFieldMsg[$k]}不能为空");
            }
            $this->data[$k] = isset($params[$ks]) ? trim($params[$ks]) : $v;
        }
        $res = DB::table($this->catTableName)->where("id", $params["id"])->update($this->data);
        if(!$res && $res != '0'){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '修改失败';
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = '修改成功';
        return $this->return_result($this->returnData);
    }

    public function delete(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        if(empty($request->id)){
            return $this->return_result(ErrorCode::$admin_enum["customized"], "ID不能为空");
        }
        if(DB::table($this->typeTableName)->where("cat_id", $request->id)->count()){
            return $this->return_result(ErrorCode::$admin_enum["customized"], "请先删除分类下的类型");
        }
        $res = DB::table($this->catTableName)->where("id", $request->id)->delete();
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '删除失败';
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = '删除成功';
        return $this->return_result($this->returnData);
    }
}